$(function() {
	$("#form").validate({
		rules : {
			"applydate" : {
				required : true
			},
			"loanuse" : {
				required : true
			},
			"loanaccount" : {
				required : true
			},
			"loanofmode" : {
				required : true
			},
			"modeofrepayment" : {
				required : true
			},
			"sourceofrepayment" : {
				required : true
			},
			"applytime" : {
				required : true
			},
			"applytimetype" : {
				required : true
			}
		},
		messages : {
			"applydate" : {
				required : '请选择申请日期!'
			},
			"loanuse" : {
				required : '请选择贷款用途!'
			},
			"loanaccount" : {
				required : '请输入贷款金额!'
			},
			"loanofmode" : {
				required : '请选择贷款方式!'
			},
			"modeofrepayment" : {
				required : '请选择还款方式!'
			},
			"sourceofrepayment" : {
				required : '请选择还款资金来源!'
			},
			"applytime" : {
				required : '请填写贷款期限!'
			},
			"applytimetype" : {
				required : '请选择贷款期限种类!'
			}
		}
	});

	$.datepicker.setDefaults($.datepicker.regional['zh-CN']);
	$('#applydate').datetimepicker({
		dateFormat : "yy-mm-dd",
		showTimepicker : false,
		changeMonth : true,
		changeYear : true
	});

	//var _clientid = document.getElementById("clientid").value;
	$_clientid = $("#clientid").val();
	$("#addKeepInfor").click(
			function() {
				if ($_clientid == "") {
					AlertDialog("提示","请先选择客户!");
				} else {
					OpenDialog(800, 400, "查找",
							"../ajax/serachEnterpriseListAjax!go_in?dostate=keepInfor&enterpriselId="
									+ $_clientid);
				}
			});
	$("#addHypothecate")
			.click(
					function() {
						if ($_clientid == "") {
							AlertDialog("提示","请先选择客户!");
						} else {
							OpenDialog(
									800,
									400,
									"查找",
									"../ajax/serachEnterpriseListAjax!go_in_hypothecate?dostate=hypothecate&enterpriselId="
											+ $_clientid);
						}
					});
	$("#addPledge").click(
			function() {
				if ($_clientid == "") {
					AlertDialog("提示","请先选择客户!");
				} else {
					OpenDialog(800, 400, "查找",
							"../ajax/serachEnterpriseListAjax!go_in_pledge?dostate=pledge&enterpriselId="
									+ $_clientid);
				}
			});
	$("#ajax").click(
			function() {
				OpenDialog(600, 400, "企业查找",
						"../ajax/serachEnterpriseListAction!loadInfor");
			});
	
	$(document).ready(function(){
		var lis = $("#applytable").find(":checkbox");
		for(var i = 0; i < lis.length; i++){
			if(lis[i].checked == true){
				$("#infor" + $(lis[i]).val()).show();
			}
		}
	});
	
	$("#applytable").delegate(":checkbox", "click", function(){
		if($(this).val() == 4 && $(this).is(":checked") == true){
			$allcb = $(":checkbox").not($(this));
			for(i = 0; i < $allcb.length; i++){
				$allcb[i].checked = false;
				$("#infor" + (i + 1)).hide();
			}
		}else if($(this).is(":checked") == true){
			$("#infor" + $(this).val()).show();
			$("#loanofmode-4").attr("checked",false);
		}else if($(this).is(":checked") == false){
			$("#infor" + $(this).val()).hide();
		}
	});
	
	$("#submit").click(function() {
		if ($("#clientid").val() === "") {
			AlertDialog("提示","请先选择客户!");
		}
	});
	
	$(document).ready(function(){
		$a = $("#loanaccount").val().match(/\d*\.?\d{0,2}/, '$1');
		var toUpper = $("#loanaccountCapital");
		toUpper.css("color", "#000");
		var upperValue = Money.toUpper($.trim($a));
		toUpper.val(upperValue);
	});
	
	$("#loanaccount").keyup(function() {
		var toUpper = $("#loanaccountCapital");
		toUpper.css("color", "#000");
		
		var num = $(this);
		//$nowMoney = $(this).val();
		
		$a = num.val().match(/\d*\.?\d{0,2}/, '$1');
		if ($a != $.trim($a)) {
			toUpper.val("输入有误!!")
			toUpper.css("color", "red");
			return;
		}
		
		//$moneyAll = $('#allMoney').val();
		//$moneyUse = $('#useMoney').val();
		//$canUseMoney = $moneyAll - $moneyUse;
		//if ($nowMoney > $canUseMoney) {
		//	num.val($canUseMoney);
		//	alert("对不起，你申请的金额已经超过了可用授信金额!");
	//	}
		
		var upperValue = Money.toUpper($.trim($a));
		toUpper.val(upperValue);
	});
});
